var MEETINGROOM_SELECTOR_UI = "";
var MEETINGROOM_SELECTOR_OBJS = null;
var MEETINGROOM_SELECTOR_ROW = null;
var MEETINGROOM_SELECTOR_START_COL = null;
var MEETINGROOM_SELECTOR_END_COL = null;
var MEETINGROOM_SELECTOR_DATE = null;

/**
 *
 */
function meetingRoomSelector(objs) {
	MEETINGROOM_SELECTOR_UI = "";
	MEETINGROOM_SELECTOR_ROW = null;
	MEETINGROOM_SELECTOR_START_COL = null;
	MEETINGROOM_SELECTOR_END_COL = null;
	MEETINGROOM_SELECTOR_DATE = null;
	MEETINGROOM_SELECTOR_OBJS = objs;
	if (MEETINGROOM_SELECTOR_UI == null || MEETINGROOM_SELECTOR_UI == "") {
		$
				.get(
						"stringRes?s=htmlBodyContentAction&file_path=oa/assistant/meetingroom_selector.html",
						{}, function(data) {
							MEETINGROOM_SELECTOR_UI = data;
							buildMeetingRoomSelector(data, objs);
						});
	} else {
		buildMeetingRoomSelector(MEETINGROOM_SELECTOR_UI, objs);
	}
}
function buildMeetingRoomSelector(data, objs) {
	var leftWidth = 150;
	var rowHeight = 18;
	var w_ = 640;
	var h_ = 250;
	$("#dialog_content").html(data);
	$("#meetingRoomSelector0").html(buildTimeSelector(rowHeight, objs));
	$("#meetingRoomSelector1").html(buildSelectorRightTop());
	$("#meetingRoomSelector2").html(
			buildSelectorLeftContent(leftWidth, rowHeight, objs));
	$("#meetingRoomSelector3").css("width", leftWidth + "px");
	$("#meetingRoomSelector1").css("width", (w_ - 16) + "px");
	$("#meetingRoomSelector2").css("width", leftWidth + "px");
	$("#meetingRoomSelector2").css("height", (h_ - 16) + "px");
	$("#meetingRoomSelector0").css("width", w_ + "px");
	$("#meetingRoomSelector0").css("height", h_ + "px");

	$("#dialogCancle").click(closeDialog);
	$("#dialogOk").click( function() {
	});
	showDialog(800, 400);

	var mrs = document.getElementById("meetingRoomSelector0");
	mrs.onscroll = function() {
		var m = document.getElementById("meetingRoomSelector0");
		setScrollTop(document.getElementById("meetingRoomSelector2"),
				getScrollTop(m));
		setScrollLeft(document.getElementById("meetingRoomSelector1"),
				getScrollLeft(m));
	}
}

function buildSelectorRightTop() {
	var s = "<table cellpadding='0' cellspacing='0'><tr>";
	for ( var i = 0; i < 49; i++) {
		s += "<td><div style='width:48px;overflow:hidden;text-align:center;padding:5px 0 5px 0;'>";
		var tmp = "";
		var x = parseInt(i / 2);
		if (i % 2 == 0) {
			if (x < 10) {
				tmp = "0" + x;
			} else {
				tmp = "" + x;
			}
			s += tmp + ":00";
		} else {
			if (x < 10) {
				tmp = "0" + x;
			} else {
				tmp = "" + x;
			}
			s += tmp + ":30";
		}
		s += "</div></td>";
	}
	s += "</tr></table>";
	return s;
}

function buildSelectorLeftContent(width_, rowHeight, objs) {
	var s = "";
	s += "<table cellpadding='0' cellspacing='0'>";
	for ( var j = 0; j < objs.length; j++) {
		var obj = objs[j];
		s += "<tr><td style='border-bottom:solid 1px #aaa;'>";
		s += "<div style='white-space:nowrap;height:" + rowHeight + "px;width:"
				+ width_ + "px;padding:5px 4px 5px 4px;overflow:hidden;'>"
				+ obj[1] + "</div>";
		s += "</td></tr>";
	}
	s += "</table>"
	return s;
}

/**
 * objs [ ['name', 0, 0.5, 4.5], ['name', 1, 3.5, 23.5], ]
 */
function buildTimeSelector(rowHeight, objs) {
	var s = "<table cellpadding='0' cellspacing='0'>";
	var color_ = "#fff";
	for ( var j = 0; j < objs.length; j++) {
		var obj = objs[j];
		s += "<tr>";
		for (i = 0; i < 50; i++) {
			if (i == 0 || i == 49) {
				var c_ = "#eee";
				if (i == 49) {
					c_ = "#fff";
				}
				s += "<td style='background:"
						+ c_
						+ ";border-bottom:solid 1px #ccc;'><div style='height:"
						+ rowHeight
						+ "px;width:24px;padding:5px 0 5px 0'>&nbsp;</div></td>";
			} else {
				var n = (i - 1) / 2;
				if (i % 2 == 0) {
					color_ = "#eee";
				} else {
					color_ = "#fff";
				}
				var isSet = false;
				for ( var k = 2; k < obj.length; k++) {
					var o = calMeetingRoomSelectorTime(obj[k]);
					if (n == o) {
						s += "<td style='background-color:"
								+ color_
								+ ";border-bottom:solid 1px #ccc;'><div style='height:"
								+ rowHeight
								+ "px;width:48px;text-align:center;padding:5px 0 5px 0'>×</div></td>";
						isSet = true;
						break;
					}
				}
				if (!isSet) {
					s += "<td style='background-color:" + color_
							+ ";border-bottom:solid 1px #ccc;'>";
					s += "<div id='MeetingRoomSelectorCheck_" + j + "_" + i + "' style='height:"
							+ rowHeight
							+ "px;width:48px;text-align:center;padding:5px 0 5px 0;cursor:pointer;'"
							+ " onclick='selectMeetingRoomTime(" + j + "," + i + ")'>";
					s += "&nbsp;</div></td>";
				}
			}
		}
		s += "</tr>";
	}
	s += "</table>";
	return s;
}

function calMeetingRoomSelectorTime(t) {
	var c = parseInt(t.substring(0, 2), 10);
	if(t.substring(3, 5) == "30") {
		c = c + 0.5;
	} 
	return c;
}

function selectMeetingRoomTime(rowN, colN) {
	if(MEETINGROOM_SELECTOR_ROW == null) {
		MEETINGROOM_SELECTOR_ROW = rowN;
	} else {
		if(MEETINGROOM_SELECTOR_ROW != rowN) {
			alert("一次不能选中两个会议室");
			return;
		}
	}
	if(MEETINGROOM_SELECTOR_START_COL == null || MEETINGROOM_SELECTOR_END_COL == null) {
		MEETINGROOM_SELECTOR_START_COL = colN;
		MEETINGROOM_SELECTOR_END_COL = colN;
	} else {
		if(MEETINGROOM_SELECTOR_START_COL == colN || MEETINGROOM_SELECTOR_END_COL == colN) {
			if(MEETINGROOM_SELECTOR_START_COL == MEETINGROOM_SELECTOR_END_COL) {
				MEETINGROOM_SELECTOR_ROW = null;
				MEETINGROOM_SELECTOR_START_COL = null;
				MEETINGROOM_SELECTOR_END_COL = null;
			} else {
				if(MEETINGROOM_SELECTOR_START_COL == colN) {
					MEETINGROOM_SELECTOR_START_COL = colN + 1;
				} else {
					MEETINGROOM_SELECTOR_END_COL = colN - 1;
				}
			}
		} else {
			if(MEETINGROOM_SELECTOR_START_COL - 1 != colN && MEETINGROOM_SELECTOR_END_COL + 1 != colN) {
				alert("时间段不能中断");
				return;
			}
			if(MEETINGROOM_SELECTOR_START_COL - 1 == colN) {
				MEETINGROOM_SELECTOR_START_COL = colN;
			}
			if(MEETINGROOM_SELECTOR_END_COL + 1 == colN) {
				MEETINGROOM_SELECTOR_END_COL = colN;
			}
		}
	}
	
	if($("#MeetingRoomSelectorCheck_" + rowN + "_" + colN).html() == "√") {
		$("#MeetingRoomSelectorCheck_" + rowN + "_" + colN).html("");
	} else {
		$("#MeetingRoomSelectorCheck_" + rowN + "_" + colN).html("√");
		$("#MeetingRoomSelectorCheck_" + rowN + "_" + colN).css("color", "green");
	}
}

function meetingRoomSelectorOk() {
	$("#MeetingEditorForm :input[@name='meetingRoomName']").val(MEETINGROOM_SELECTOR_OBJS[MEETINGROOM_SELECTOR_ROW][1]);
	$("#MeetingEditorForm :input[@name='meeting_Room_id_']").val(MEETINGROOM_SELECTOR_OBJS[MEETINGROOM_SELECTOR_ROW][0]);
	var startTemp = parseInt((MEETINGROOM_SELECTOR_START_COL - 1) / 2);
	var startTime = "" + ((startTemp < 10) ? "0" + startTemp : startTemp)
		+ ":" + (((MEETINGROOM_SELECTOR_START_COL - 1) % 2 == 0)? "00":"30");
	$("#MeetingEditorForm :input[@name='start_time_']").val(startTime);
	var endTemp = parseInt((MEETINGROOM_SELECTOR_END_COL) / 2);
	var endTime = "" + ((endTemp < 10) ? "0" + endTemp : endTemp) 
		+ ":" + (((MEETINGROOM_SELECTOR_END_COL) % 2 == 0)? "00":"30");
	$("#MeetingEditorForm :input[@name='end_time_']").val(endTime);
	$("#MeetingEditorForm :input[@name='date_']").val(getCurrentDate());
	closeDialog();
}

function setScrollTop(el, x) {
	el.scrollTop = Math.round((el.scrollHeight - el.offsetHeight) * x); 
	// 16: the horizontal scrollbar
}
function getScrollTop(el) {
	return el.scrollTop / (el.scrollHeight - el.offsetHeight + 16); 
	// 16: the horizontal scrollbar
}
function setScrollLeft(el, x) {
	el.scrollLeft = Math.round((el.scrollWidth - el.offsetWidth) * x); 
	// 16: the horizontal scrollbar
}
function getScrollLeft(el) {
	return el.scrollLeft / (el.scrollWidth - el.offsetWidth + 16); 
	// 16: the horizontal scrollbar
}

 function getCurrentDate()   
 {   
     var today = new Date();   
     var day = today.getDate();   
     var month = today.getMonth() + 1;   
     var year = today.getFullYear();   
     var date = year + "-" + month + "-" + day;   
     return date;
 }  
